﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;


namespace DbSharp.SupportLib.Crypto
{
    public class RsaCryptoService
    {
        public static RSACryptoServiceProvider PrepareRSA()
        {
            const int PROVIDER_RSA_FULL = 1;
            const string CONTAINER_NAME = "SpiderContainer";
            CspParameters cspParams;
            cspParams = new CspParameters(PROVIDER_RSA_FULL);
            cspParams.KeyContainerName = CONTAINER_NAME;
            cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
            cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
            return new RSACryptoServiceProvider(cspParams);
        }


        public string EncryptData(string data2Encrypt, string publicKeyFile)
        {
            RSACryptoServiceProvider rsa;
            using (rsa = PrepareRSA())
            {
                //StreamReader reader = new StreamReader(@"C:\\RsaPublicKey.xml");
                StreamReader reader = new StreamReader(publicKeyFile);
                string publicOnlyKeyXML = reader.ReadToEnd();
                rsa.FromXmlString(publicOnlyKeyXML);
                reader.Close();

                //read plaintext, encrypt it to ciphertext
                byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
                byte[] cipherBytes = rsa.Encrypt(plainBytes, false);
                return Convert.ToBase64String(cipherBytes);
            }
        }



        
        public string DecryptData(string data2Decrypt, string privateKeyFile)
        {
            RSACryptoServiceProvider rsa;
            using (rsa = PrepareRSA())
            {
                byte[] cipherBytes = Convert.FromBase64String(data2Decrypt);

                //StreamReader reader = new StreamReader(@"C:\\RsaPrivateKey.xml");
                StreamReader reader = new StreamReader(privateKeyFile);
                string publicPrivateKeyXML = reader.ReadToEnd();
                rsa.FromXmlString(publicPrivateKeyXML);
                reader.Close();

                //read ciphertext, decrypt it to plaintext
                byte[] plain = rsa.Decrypt(cipherBytes, false);
                return System.Text.Encoding.UTF8.GetString(plain);
            }
        }


        public void SaveKey(string privateKeyFile, string publicKeyFile)
        {
            RSACryptoServiceProvider rsa;
            using (rsa = PrepareRSA())
            {
                //provide public and private RSA params
                //StreamWriter writer = new StreamWriter(@"C:\\privatekey.xml");
                StreamWriter writer = new StreamWriter(privateKeyFile);
                string publicPrivateKeyXML = rsa.ToXmlString(true);
                writer.Write(publicPrivateKeyXML);
                writer.Close();

                //provide public only RSA params
                //writer = new StreamWriter(@"C:\\publickey.xml");
                writer = new StreamWriter(publicKeyFile);
                string publicOnlyKeyXML = rsa.ToXmlString(false);
                writer.Write(publicOnlyKeyXML);
                writer.Close();
            }
        }
    }
}
